package org.itboys.core.service.mysql.front;


import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.product.SundayProductDto;
import org.itboys.core.entity.mysql.SundayProduct;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_产品_service_weixin
 */
@Service
public class SundayFrontProductService extends BaseMySqlService<SundayProduct> {
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SystemDictionaryService dictionaryService;
    /**
     * 查找单个
     * @param productId
     * @return
     */
    public SundayProductDto findOne(Long productId,Long memberId) throws Exception {
        //throw new ErrorException(ExceptionEnum.COMMENEXCPTION.getValue());
        SundayProduct product=super.find(productId);
        return packageDtoSingle(product,memberId);
    }

    /**
     * 查询
     * @param request
     * @return
     */
    public List<SundayProductDto> selectProduct(HttpServletRequest request,Integer type) {
        List<SundayProductDto> dtos=null;
        Map<String,Object> param=ParamUtil.packageMysqlPageParam(request,"categoryId","name","isRecommend","isHot","parentId");
        if(type==1){
            dtos=packageDtoMuiltSoulou(super.select(param));
        }else if(type==2){
            dtos=packageDtoMuiltYouxuan(super.select(param));
        }
        return dtos;
    }

    public SundayProductDto packageDtoSingle(SundayProduct product,Long memberId){
        SundayProductDto dto = null;
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("outClassId",product.getId());
        param.put("outClassName",SundayProduct.class.getSimpleName());
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        param.clear();
        param.put("otherId",product.getId());
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("type",SundayCommonConstants.collect_type_product);
        param.put("memberId",memberId);
      /*  dto=new SundayProductDto(product.getId(), product.getCreateTime(), product.getUpdateTime(),
                product.getCategoryName(),product.getName(),product.getTotalPrice(), product.getAveragePrice(),
                product.getAddress(),product.getOpenTime(),product.getTraffic(), product.getRecommend(),
                0,0,  rollImages);*/
        return dto;
    }

    //组装搜楼列表
    public List<SundayProductDto> packageDtoMuiltSoulou(List<SundayProduct> products) {
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        //组装标签
        List<SystemDictionary> dictionaries=dictionaryService.getDictionaryByKey("house_tag");

        for(SundayProduct product:products){
            /*dtos.add(new SundayProductDto(product.getId(), product.getCreateTime(), product.getUpdateTime(), product.getName(), product.getViceName(),
                    imageRoot+product.getBackImage(), product.getSaleStatus(), tags));*/
        }
        return dtos;
    }
    //组装优选列表
    public List<SundayProductDto> packageDtoMuiltYouxuan(List<SundayProduct> products) {
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        for(SundayProduct product:products){
            Map<String,Object> param = new HashMap<String, Object>();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("outClassId",product.getId());
            param.put("outClassName",SundayProduct.class.getSimpleName());
            List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
            /*dtos.add(new SundayProductDto(product.getId(),product.getCreateTime(), product.getUpdateTime(), product.getName(),
                    product.getAcreage(), product.getPriceRange(), rollImages,imageRoot+product.getDefaultImage()));*/
        }

        return dtos;
    }
}
